package com.blog.service.Impl;

import com.blog.common.ServerResponse;
import com.blog.dao.CategoryMapper;
import com.blog.pojo.Category;
import com.blog.service.ICategoryService;
import com.sun.security.ntlm.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author
 * @create 2018-06-07 18:32
 **/
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 查询所有分类信息
     * @return
     */
    @Override
    public List<Category> list() {
        return categoryMapper.selectList();
    }

    @Override
    public ServerResponse save(String[] idArr, String[] soreArr, String[] nameArr) {
       //遍历
        for (int i = 0; i < idArr.length; i++) {
            //判断该数据是更新还是插入，根据id是否为空来判断
            int count = 0;
            if (StringUtils.isEmpty(idArr[i])) {
                //该数据是插入
                count = categoryMapper.insert(soreArr[i],nameArr[i]);
                if (count == 0) {
                    return ServerResponse.error("插入数据失败");
                }
            }else {
                //更新
               count =  categoryMapper.update(idArr[i], soreArr[i],nameArr[i]);
                if (count == 0) {
                    return ServerResponse.error("更新数据失败");
                }
            }
        }
        //判断返回值是否为0,0则保存或者更新失败，返回错误信息
        return ServerResponse.success();
    }

    /**
     * 批量删除分类信息
     * @param idArr
     * @return
     */
    @Override
    public ServerResponse deleteById(String[] idArr) {
        for (int i = 0; i < idArr.length; i++) {
            int count = categoryMapper.deleteById(idArr[i]);
            if (count == 0) {
                return ServerResponse.error("删除数据失败！");
            }
        }
        return ServerResponse.success();
    }
}
